package cn.homework.web.servlet;

import cn.homework.domain.Admin;
import cn.homework.domain.Customers;
import cn.homework.service.LoginService;
import cn.homework.service.impl.LoginServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author ACT
 */
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String demand = request.getParameter("demand");
//        System.out.println("demandLogin" + demand);
        switch (demand) {
            case "customer": {
                customerLogin(request, response);
                break;
            }
            case "admin": {
                adminLogin(request, response);
                break;
            }
            case "customerRetrievePassword": {
                customerRetrievePassword(request, response);
                break;
            }
            case "adminRetrievePassword": {
                adminRetrievePassword(request, response);
            }
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 前台用户登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void customerLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据
        Map<String, String[]> map = request.getParameterMap();
        //封装数据
        Customers customers = new Customers();
        try {
            BeanUtils.populate(customers, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用service查询用户
        LoginService service = new LoginServiceImpl();
        Customers loginCustomer = service.loginCustomers(customers);
        if (loginCustomer != null) {
            //登录成功
            //将用户存储到session中
            HttpSession session = request.getSession();
            session.setAttribute("loginCustomer", loginCustomer);
            //重定向
            response.sendRedirect(request.getContextPath() + "/productServlet?src=login");
        } else {
            //登录失败
            request.setAttribute("loginFailed_msg", "用户名或密码错误");
            request.getRequestDispatcher("/CommodityHome.jsp").forward(request, response);
        }
    }

    /**
     * 后台用户登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void adminLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据
        Map<String, String[]> map = request.getParameterMap();
        //封装数据
        Admin admin = new Admin();
        try {
            BeanUtils.populate(admin, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用service查询用户
        LoginService service = new LoginServiceImpl();
        Admin loginUser = service.loginAdmin(admin);
        if (loginUser != null) {
            //登录成功
            //将用户存储到session中
            HttpSession session = request.getSession();
            session.setAttribute("loginUser", loginUser);
            //重定向
//            response.sendRedirect(request.getContextPath() + "/BackstageHome.jsp");
            response.sendRedirect(request.getContextPath() + "/backstageCustomerServlet?demand=show");
        } else {
            //登录失败
            request.setAttribute("loginFailed_msg", "用户名或密码错误");
            request.getRequestDispatcher("/BackstageHomeLogin.jsp").forward(request, response);
        }
    }

    /**
     * 前台用户忘记密码操作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void customerRetrievePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Customers customers = new Customers();
        try {
            BeanUtils.populate(customers, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        LoginService service = new LoginServiceImpl();
        Customers customer = service.judgmentCustomer(customers);
        if (customer != null) {
            request.setAttribute("customerPassword", "密码为：" + customer.getPassword());
            request.getRequestDispatcher("/CommodityForgetPassword.jsp").forward(request, response);
        } else {
            request.setAttribute("informationError_msg", "查询无此用户");
            request.getRequestDispatcher("/CommodityForgetPassword.jsp").forward(request, response);
        }
    }

    /**
     * 后台用户忘记密码操作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void adminRetrievePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Admin admin = new Admin();
        try {
            BeanUtils.populate(admin, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        LoginService service = new LoginServiceImpl();
        Admin user = service.judgmentAdmin(admin);
        if (user != null) {
            request.setAttribute("adminPassword", "密码为：" + user.getPassword());
            request.getRequestDispatcher("/BackstageForgetPassword.jsp").forward(request, response);
        } else {
            request.setAttribute("informationError_msg", "查询无此用户");
            request.getRequestDispatcher("/BackstageForgetPassword.jsp").forward(request, response);
        }
    }
}
